<?php
namespace app\service;

use app\repository\ClazzRepository;

class ClazzService {
    public static $sessionKey = 'user';

    /**
     * 将日期字符串，转成日期int
     * @input 2022-01-18 
     * @output 20220118
     * */
    static public function dateStringToDateInt($dateString) {
        if (!isset($dateString) || empty($dateString)) {
            return null;
        }
        // 设置默认时区为上海
        date_default_timezone_set("Asia/Shanghai");
        // 将日期字符中转成时间戳 '2022-01-18' -> 1579276800
        $timestamp = strtotime($dateString);
        // 将时间戳再格式化为日期字符串 1579276800 -> '20220118'
        // 将日期字符串转成int '20220118'
        return (int) date('Ymd', $timestamp);
    }

    function getById($id) {
        $clazzRepository = new ClazzRepository;
        return $clazzRepository->findById($id);
    }

    /**
     * 
     * 获取当前登录的用户
     * 
     * */
    static function getCurrentLoginUser() {
        return self::isLogin() ? $_SESSION[self::$sessionKey] : null;
    }


    /**
     * 
     * 判断用户是否已经登录
     * 
     * */
    static function isLogin() {
        if (!isset($_SESSION[self::$sessionKey]) || is_null($_SESSION[self::$sessionKey])) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * @param $page 第几页 
     * @param $size 每页大小
     * @param $name 班级名称
     * @param $entranceDateBegin 入学日期 起始
     * @param $entranceDateEnd 入学日期结束
     * @param $userName 班主任姓名
     * */
    public function page($page, $size, $name, $entranceDateBegin, $entranceDateEnd, $userName) {
        $clazzRepository = new ClazzRepository;
        $clazzRepository->nameContains($name);
        $entranceDateBeginInt = self::dateStringToDateInt($entranceDateBegin);
        $entranceDateEndInt = self::dateStringToDateInt($entranceDateEnd);

        // 终止时间小于开始时间时，进行反转
        if (isset($entranceDateBeginInt) && isset($entranceDateEndInt) && ($entranceDateBeginInt > $entranceDateEndInt)) {
            $temp = $entranceDateBeginInt;
            $entranceDateBeginInt = $entranceDateEndInt;
            $entranceDateEndInt = $temp;
        }

        $clazzRepository->entranceDateGreaterThanOrEqauls($entranceDateBeginInt);
        $clazzRepository->entranceDateLessThanOrEqauls($entranceDateEndInt);
        $clazzRepository->userNameContains($userName);
        return $clazzRepository->page($page, $size);
    }

    /**
     * 新建
     * @return 正确执行， true; 发生错误 -》 数据库报错信息
     * */
    public function save($clazz) {
        $currentUser = $this->getCurrentLoginUser();
        $clazz->createUser->id = $currentUser['id'];
        $clazz->entranceDate = $this->dateStringToDateInt($clazz->entranceDate);
        $clazzRepository = new ClazzRepository();
        return $clazzRepository->save($clazz);
    }

    public function update($id, $name, $entranceDate, $numberOfStudents, $userId) {
        $clazzRepository = new ClazzRepository();
        return $clazzRepository->update($id, $name, $this->dateStringToDateInt($entranceDate), $numberOfStudents, $userId);
    }
}